package com.zhongx.xingcd.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class FilterUtil implements Filter {
    Logger log=LoggerFactory.getLogger(this.getClass());

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) arg0;
        HttpServletResponse response = ((HttpServletResponse) arg1);
        String m=request.getMethod();
        if(!"GET".equals(m)&&!"POST".equals(m)&&!"OPTIONS".equals(m)){
            System.out.println("GET or POST only  ");
            System.out.println(m);
            response.setHeader("Allow", "GET,POST,OPTIONS");
            response.setStatus(405);
            return;
        }
        arg2.doFilter(arg0, arg1);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }

}